package com.xingfudazi.web.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xingfudazi.app.domain.AppAct;
import com.xingfudazi.app.domain.AppCollect;
import com.xingfudazi.app.service.IAppActService;
import com.xingfudazi.app.service.IAppCollectService;
import com.xingfudazi.common.core.controller.BaseController;
import com.xingfudazi.common.core.domain.AppCollectable;
import com.xingfudazi.common.core.domain.Result;
import com.xingfudazi.common.core.page.TableDataInfo;
import com.xingfudazi.common.exception.ServiceException;
import com.xingfudazi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author YuJiaMing
 * date 2024年09月27日 13:14
 */
@RestController
@RequestMapping("/app/collect")
public class AppCollectController extends BaseController {

    @Autowired
    private IAppCollectService appCollectService;


    @Autowired
    private IAppActService appActService;

    /**
     * 添加收藏
     */
    @PostMapping
    public Result<?> add(@RequestBody AppCollect appCollect) {
        Long userId = SecurityUtils.getUserId();
        appCollect.setNid(userId);

        LambdaQueryWrapper<AppCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppCollect::getNid, userId);
        wrapper.eq(AppCollect::getBid, appCollect.getBid());
        if (this.appCollectService.count(wrapper) > 0) {
            throw new ServiceException("不要重复收藏");
        }

        this.appCollectService.save(appCollect);
        return Result.success();
    }


    /**
     * 删除id
     */
    @DeleteMapping("{id}")
    public Result<?> delete(@PathVariable Long id) {
        this.appCollectService.removeById(id);
        return Result.success();
    }


    /**
     * 分页查询收藏
     *
     * @param type
     * @return
     */
    @GetMapping("page")
    public Result<Page<AppCollectable>> page(String type) {
        startPage();
        LambdaQueryWrapper<AppCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(AppCollect::getCreateTime);
        List<AppCollect> list = this.appCollectService.list(wrapper);
        TableDataInfo<AppCollect> dataTable = getDataTable(list);

        Page<AppCollectable> page = new Page<>();
        page.setTotal(dataTable.getTotal());

        List<Long> idSet = list.stream().map(AppCollect::getBid).collect(Collectors.toList());

        if (AppCollect.TYPE_ACTIVE.equals(type)) {
            Map<Long, AppAct> actMap = appActService.listByIds(idSet).stream().collect(Collectors.toMap(AppAct::getActId, Function.identity()));
            List<AppCollectable> appActs = list.stream().map(item -> actMap.get(item.getBid())).collect(Collectors.toList());
            page.setRecords(appActs);
        }

        return Result.success(page);

    }


    public static void main(String[] args) {
        List<? extends AppCollectable> a1 = getAct();
        List<AppCollectable> appActs = new ArrayList<>(getAct());
    }

    public static List<AppAct> getAct() {
        return new ArrayList<AppAct>();
    }

}
